﻿using Api.Common;
using AppManager.BLL;
using AppManager.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace PushTask
{
    public class SignalrPush
    {
        public static void Send()
        {
            LogHelper.WriteCreateNewLog(LogType.signalrInfo, "开始执行");

            Stopwatch sw = Stopwatch.StartNew();
            var messageList = MessageBLL.GetSendingList();
            sw.Stop();
            var alltime = sw.Elapsed;

            if (messageList.Count() == 0)
            {
                LogHelper.WriteCreateNewLog(LogType.signalrInfo, "没有需要发送的消息list.Count==0");
                return;
            }

            List<MessageDevice> successList = new List<MessageDevice>();
            List<MessageDevice> errorList = new List<MessageDevice>();

            foreach (var msg in messageList)
            {
                var project = msg.Project;
                if (!project.IsUseSignalr) continue;

                var messageId = msg.Id;

                var signalrdevices = msg.Devices.Where(s => s.DeviceType == DeviceType.android && !s.IsUseGoogle);

                try
                {
                    #region signalr 推送
                    SignalrSend(msg, signalrdevices, successList, errorList);
                    #endregion
                }
                catch (Exception ex)
                {
                    LogHelper.WriteLongLog(LogType.signalrError, "循环messageList：" + ex.Message);
                }
                finally
                {
                    MessageDeviceBLL.UpdateStatus(true, successList);
                    MessageDeviceBLL.UpdateStatus(false, errorList);
                }
            }

            LogHelper.WriteCreateNewLog(LogType.signalrInfo, "结束执行");
        }

        //一個重要的問題是怎麼解決連接之後的console
        private static void SignalrSend(Message message, IEnumerable<MessageDevice> devices, List<MessageDevice> successList, List<MessageDevice> errorList)
        {
            try
            {
                devices = from a in devices where !string.IsNullOrEmpty(a.DeviceStr) select a;
                var list = devices.Select(s => s.DeviceStr).ToArray();

                var obj = JsonConvert.DeserializeObject(message.Data);
                var str = new SignalrData
                {
                    content = message.Content,
                    data = obj,
                    mid = message.Id
                }.ToString();
                //調取服務器發送推送消息                        
                new SendSignalrMessage().Send(str, list);
                successList.AddRange(devices);
            }
            catch (Exception ex)
            {
                errorList.AddRange(devices);
                LogHelper.WriteLongLog(LogType.signalrError, "signalrSend循环messageList：" + ex.Message);
            }
            finally
            {
                MessageDeviceBLL.UpdateStatus(true, successList);
                MessageDeviceBLL.UpdateStatus(false, errorList);
            }

        }
    }
}
